import * as CryptoJS from 'crypto-js';

/**
 * 解密AES
 * @param enc
 * @param secret
 */
export function decrypt(enc: string, secret: string) {
  const key = CryptoJS.MD5(secret);
  const dec = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(enc), key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  return CryptoJS.enc.Utf8.stringify(dec);
}

/**
 * 解密AES
 * @param data
 * @param secret
 */
export function encrypt(data: any, secret: string) {
  const key = CryptoJS.MD5(secret);
  const src = JSON.stringify(data);
  const enc = CryptoJS.AES.encrypt(src, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  return enc.ciphertext.toString();
}
